package com.realbrand.net.realbrandpay.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.View;

import com.realbrand.net.realbrandpay.bean.ShopcarBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/6/26.
 */

public class DBManger {
    private Context context;
    private DataBase dataBase;
    private SQLiteDatabase db;

    public DBManger(Context context) {
        this.context = context;
        dataBase = new DataBase(context);
        db = dataBase.getWritableDatabase();
    }

    //向数据库添加数据
    public void insertsqllist(ShopcarBean bean) {
        String sql = "insert into foodstable(name,stroe,adress,price,scan,image,activationtime,productionAddress,commodityProfile,applyUser,scanState,scanFrequency,scanTime) values ('" + bean.getName() + "','" + bean.getStore() + "','" + bean.getAdress()
                + "','" + bean.getPrice() + "','" + bean.getScan() + "','" + bean.getDrawableId()
                + "','" + bean.getActivationTime() + "','" + bean.getProductionAdress() + "','"
                + bean.getCommodityProfile() + "','" + bean.getApplyUser() + "','" + bean.getScanStatte() + "','" + bean.getScanFrequency() + "','" + bean.getScanTime() + "')";
        SQLiteDatabase sd = dataBase.getWritableDatabase();
        sd.execSQL(sql);
        sd.close();
    }

    //  取出数据库里保存的数据
    public List<ShopcarBean> query() {

        ArrayList<ShopcarBean> bean = new ArrayList<ShopcarBean>();
        Cursor c = queryTheCursor();
        while (c.moveToNext()) {
            ShopcarBean foods = new ShopcarBean();
            foods.name = c.getString(c.getColumnIndex("name"));
            foods.store = c.getString(c.getColumnIndex("stroe"));
            foods.adress = c.getString(c.getColumnIndex("adress"));
            foods.price = c.getString(c.getColumnIndex("price"));
            foods.scan = c.getString(c.getColumnIndex("scan"));
            foods.drawableId = c.getString(c.getColumnIndex("image"));
            foods.activationTime = c.getString(c.getColumnIndex("activationtime"));
            foods.productionAdress = c.getString(c.getColumnIndex("productionAddress"));
            foods.commodityProfile = c.getString(c.getColumnIndex("commodityProfile"));
            foods.applyUser = c.getString(c.getColumnIndex("applyUser"));
            foods.scanStatte = c.getString(c.getColumnIndex("scanState"));
            foods.scanFrequency=c.getString(c.getColumnIndex("scanFrequency"));
            foods.scanTime=c.getString(c.getColumnIndex("scanTime"));
            bean.add(foods);
        }
        c.close();
        return bean;
    }

    //删除某类商品数据
    public void deleteOne(String name) {
        String sql = "delete from foodstable where name='" + name + "'";
        db.execSQL(sql);
        db.close();
    }

    //删除某一行数据
    public void deleteOneData(int msgId) {
        String sql = "delete from foodstable where msgID='" + msgId + "'";
        db.execSQL(sql);
        db.close();
    }

    //删除数据
    public void deletesqlsql(String scan) {
        String sql = "delete from foodstable where scan='" + scan + "'";
        db.execSQL(sql);
        db.close();
    }

    //删除数据库表的所有数据
    public void deleteSQL() {
        String sql = "delete from foodstable";
        db.execSQL(sql);
        db.close();
    }

    //查询某一条数据
    public ShopcarBean selectScan(String scan) {
        String sql = "select*from foodstable where scan='" + scan + "'";
        Cursor cursor = db.rawQuery(sql, null);
        ShopcarBean bean = new ShopcarBean();
        while (cursor.moveToNext()) {
            bean.name = cursor.getString(cursor.getColumnIndex("name"));
            bean.store = cursor.getString(cursor.getColumnIndex("stroe"));
            bean.adress = cursor.getString(cursor.getColumnIndex("adress"));
            bean.price = cursor.getString(cursor.getColumnIndex("price"));
            bean.scan = cursor.getString(cursor.getColumnIndex("scan"));
            bean.drawableId = cursor.getString(cursor.getColumnIndex("image"));
            bean.activationTime = cursor.getString(cursor.getColumnIndex("activationtime"));
            bean.productionAdress = cursor.getString(cursor.getColumnIndex("productionAddress"));
            bean.commodityProfile = cursor.getString(cursor.getColumnIndex("commodityProfile"));
            bean.applyUser = cursor.getString(cursor.getColumnIndex("applyUser"));
            bean.scanStatte = cursor.getString(cursor.getColumnIndex("scanState"));
            bean.scanFrequency=cursor.getString(cursor.getColumnIndex("scanFrequency"));
            bean.scanTime=cursor.getString(cursor.getColumnIndex("scanTime"));
        }
        cursor.close();
        return bean;
    }

    //查询数据库中是否有该二维码
    public boolean scanIsExist(String scan) {
        String sql = "select*from foodstable where scan='" + scan + "'";
        Cursor cursor = db.rawQuery(sql, null);
        int scan1 = cursor.getColumnIndex("scan");
        if (scan1 == -1) {
            //表示没有
            return true;
        } else {
            return false;
        }
    }

    public boolean search_scan(String str) {
        Cursor cursor = db.rawQuery(
                "select * from foodstable where  scan =? ",
                new String[]{str});
        while (cursor.moveToNext()) {
            db.close();
            return true;// //有城市在数据库已存在，返回true
        }
        db.close();
        return false;// //在数据库以前存在 false
    }


    public Cursor queryTheCursor() {
        Cursor c = db.rawQuery("SELECT * FROM " + DataBase.TABLE_NAME,
                null);
        return c;
    }

    public int allCaseNum() {
        String sql = "select count(*) from foodstable";
        Cursor cursor = db.rawQuery(sql, null);
        cursor.moveToFirst();
        int count = cursor.getInt(0);
        cursor.close();
        return count;
    }


}
